package com.study.services;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.jdao.ICustomerDAO;
import com.study.model.jdao.IDictionaryDAO;
import com.study.model.jdao.ILinkmanDAO;
import com.study.model.mdao.LinkmanMapper;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.UserMDAO;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Dictionary;
import com.study.model.pojo.Linkman;

@Service
@Transactional(rollbackOn = Exception.class)
public class LinkmanService {
	@Autowired
	ILinkmanDAO linDAO;

	@Autowired
	LinkmanMapper mapper;
	@Autowired
	IDictionaryDAO iDictionaryDAO;

	@Autowired
	ICustomerDAO iCustomerDAO;
	@Autowired
	LogforcustDAO logforcustDAO;
	@Autowired
	UserMDAO udao;

	public Linkman findById(int id) {
		return linDAO.findByLinId(id);
	}

	public List<JSONObject> selectAll() {
		return mapper.selectAll();
	}

	/**
	 * Wcx 带条件的查询
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param name
	 * @param selects
	 * @return
	 */
	public List<JSONObject> selectToshow(int pageNum, int pageSize, String name, String selects,String useid) {
		String sex = null;
		String type = null;
		PageHelper.startPage(pageNum, pageSize);
		if (selects.length() > 0 && selects.startsWith("122")) {
			sex = selects;
		} else if (selects.length() > 0 && selects.startsWith("121")) {
			type = selects;
		}
		String input = "%" + name + "%";
		List<JSONObject> selectwhere = new ArrayList<JSONObject>();
        if ("6".equals(useid)) {
        	selectwhere=mapper.selectToshow(sex, type, input);
		}else {
			selectwhere=mapper.selectToshowUse(sex, type, input,useid);
		}
		return selectwhere;
	}

	/**
	 * 根据id查找联系人
	 * 
	 * @param linid
	 * @return
	 */
	public JSONObject selectByID(int linid) {
		return mapper.selectBylinid(linid);
	}

	/**
	 * 修改联系人信息
	 * 
	 * @param id
	 * @param phone
	 * @param custid 
	 * @return
	 */
	public boolean updatelin(String linid, String phone, String name, String linkmantype, String cust, String business,
			String sexs, String appellation, String type, String dept, String duty, String email, String workphone,
			String zip, String homephone, String wechat, String like, String idtype, String idnum, String remark,
			String photo, String habit, String interest, String trait, String social, String income,
			String consumption, String custid,String useid) {
		boolean flag = false;
		 String names = udao.getfindUsreid(useid).getString("use_name");
		int classify = Integer.parseInt(linkmantype);
		//int cusid = Integer.parseInt(cust);
		try {
			int lintype = Integer.parseInt(type);
			System.out.println(lintype+"  ds");
	     JSONObject selectBycusid = mapper.selectBycusid(Integer.parseInt(custid));
	     System.out.println(selectBycusid +"  " + custid);
	     System.out.println("11dasda");
	     System.err.println("   "+Integer.parseInt(linid));
	     System.err.println((int)selectBycusid.get("lin_id")+"dds");
	      if (1232==lintype&&Integer.parseInt(linid) == (int)selectBycusid.get("lin_id")) {
		       flag=false;
		       System.out.println("dasda");
	        }
	      else {
			Linkman linkman = linDAO.findByLinId(Integer.parseInt(linid));
			linkman.setLinPhone(phone);
			linkman.setLinName(name);
			Dictionary dictionary2 = iDictionaryDAO.findByDicId(classify);
			linkman.setDictionary2(dictionary2);
			System.out.println(custid);
			Customer customer = iCustomerDAO.findByCusId(Integer.parseInt(custid));
			linkman.setCustomer(customer);
			linkman.setLinResponsible(business);
			int sex = Integer.parseInt(sexs);
			Dictionary dictionary3 = iDictionaryDAO.findByDicId(sex);
			linkman.setDictionary3(dictionary3);
			linkman.setLinTitle(appellation);
			 if(1231==lintype) {
	            	mapper.updatelintype(Integer.parseInt(custid));
				}
			 
			Dictionary dictionary4 = iDictionaryDAO.findByDicId(lintype);
			linkman.setDictionary4(dictionary4);
			linkman.setLinDepartment(dept);
			linkman.setLinDuty(duty);
			linkman.setLinWorkPhone(workphone);
			linkman.setLinEmail(email);
			linkman.setLinHomePhone(homephone);
			linkman.setLinWeChat(wechat);
			linkman.setLinHobby(like);
			linkman.setLinIdCard(idnum);
			linkman.setLinRemark(remark);
			linkman.setLinImg(photo);
			linkman.setLinHabit(habit);
			linkman.setLinInterest(interest);
			linkman.setLinTrait(trait);
			linkman.setLinCharacter(social);
			linkman.setLinYearIncome(income);
			linkman.setLinShow(1);
			int linconsumption = Integer.parseInt(consumption);
			Dictionary dictionary1 = iDictionaryDAO.findByDicId(linconsumption);
			linkman.setDictionary1(dictionary1);
			Linkman sava = linDAO.save(linkman);
			
			 String logMessage ="员工"+names+"在"+new Date()+"修改了联系人"+name+"的基本信息";
				logforcustDAO.insert(Integer.parseInt(custid), new Date(), Integer.parseInt(useid), 2204, logMessage, "");
			flag = true;
	      }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 添加一条联系人信息
	 * @param custid 
	 */
	public boolean addlinkman(String phone, String name, String linkmantype, String business, String sexs,
			String appellation, String type, String dept, String duty, String email, String workphone, String zip,
			String homephone, String wechat, String like, String idtype, String idnum, String remark, String photo,
			String habit, String interest, String trait, String social, String income, String consumption, String custid) {
		boolean flag = false;
		try {
			Linkman linkman = new Linkman();
			int classify = Integer.parseInt(linkmantype);
			/*int cusid = Integer.parseInt(cust);*/
			
			int linconsumption = Integer.parseInt(consumption);
			int lintype = Integer.parseInt(type);
			int sex = Integer.parseInt(sexs);
			Dictionary dictionary2 = iDictionaryDAO.findByDicId(classify);

			Customer customer = iCustomerDAO.findByCusId(Integer.parseInt(custid));
            if(1231==lintype) {
            	mapper.updatelintype(Integer.parseInt(custid));
			}
			
			Dictionary dictionary3 = iDictionaryDAO.findByDicId(sex);
			Dictionary dictionary4 = iDictionaryDAO.findByDicId(lintype);

			Dictionary dictionary1 = iDictionaryDAO.findByDicId(linconsumption);
			linkman.setDictionary4(dictionary4);
			linkman.setLinDepartment(dept);
			linkman.setDictionary2(dictionary2);
			linkman.setDictionary3(dictionary3);
			linkman.setLinTitle(appellation);
			linkman.setLinDuty(duty);
			linkman.setLinWorkPhone(workphone);
			linkman.setCustomer(customer);
			linkman.setLinResponsible(business);
			linkman.setLinEmail(email);
			linkman.setLinHomePhone(homephone);
			linkman.setLinWeChat(wechat);
			linkman.setLinHobby(like);
            linkman.setLinShow(1);
			linkman.setLinIdCard(idnum);
			linkman.setLinRemark(remark);
			linkman.setLinPhone(phone);
			linkman.setLinName(name);
			linkman.setLinImg(photo);
			linkman.setLinHabit(habit);
			linkman.setLinInterest(interest);
			linkman.setLinTrait(trait);
			linkman.setLinCharacter(social);
			linkman.setLinYearIncome(income);
			linkman.setDictionary1(dictionary1);
			Linkman sava = linDAO.save(linkman);
			flag = true;
		} catch (Exception e) {
			System.out.println("新增联系人失败");
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 根据名字找到联系人
	 */
	public Linkman selectByName(String name) {
		return linDAO.findByLinName(name);
	}
	/**
	 * 根据id找到联系人
	 * @param parseInt
	 * @return
	 */
	public Linkman selectById(int parseInt) {
		return linDAO.findByLinId(parseInt);
	}

	public int addlinmanforcus(String custer, String famoney, int type, String cusid, String ad_id) {
		
		return mapper.addlinmanforcus(custer, famoney, type, cusid, ad_id);
	}
    /**
     * 客户视图展示
     * @param id
     * @return
     */
	public List<JSONObject> selectOne(String id) {
		// TODO Auto-generated method stub
		return mapper.selectOne(id);
	}

}
